package com.longrise.umeng.share;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.longrise.umeng.R;
import com.longrise.umeng.share.bean.ShareParams;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.File;

import static com.longrise.umeng.share.DialogTools.showToast;

/**
 * Created by Administrator on 2017/12/18.
 * 用于友盟分享的dialog
 */

public class ShareDialog extends ShareBaseDialog implements View.OnClickListener {

    private String ROOT_FILE_DIR = "";

    /**
     * 存放app的icon
     */
    public  String ICON_DIR = "";

    private static final String TAG = "ShareDialog";

    private ShareCallback mCallback;

    private LinearLayout mLLWeChat;
    private LinearLayout mLLWeChatMoments;
    private LinearLayout mLLQQ;
    private LinearLayout mLLQQZone;

    private ImageView mIvClose;

    private ShareParams mShareParams;

    private Context context;

    public ShareDialog(Context context) {
        super(context, R.style.VideoListDialog);
        this.context = context;
        setCanceledOnTouchOutside(false);
    }

    public void setShareParame(ShareParams shareParame) {
        this.mShareParams = shareParame;
    }

    @Override

    public int getLayoutResourceId(Bundle bundle) {
        return R.layout.dialog_share;
    }

    @Override
    public void init() {
        mLLWeChat = (LinearLayout) findViewById(R.id.shareapp_wechat_ll);
        mLLWeChatMoments = (LinearLayout) findViewById(R.id.shareapp_pengyouquan_ll);
        mLLQQ = (LinearLayout) findViewById(R.id.shareapp_qq_ll);
        mLLQQZone = (LinearLayout) findViewById(R.id.shareapp_qzone_ll);
        mIvClose = (ImageView) findViewById(R.id.iv_share_dismiss);
        ROOT_FILE_DIR = getDiskFileDir();
        ICON_DIR = ROOT_FILE_DIR + "/icon";
        regEvent();
    }

    @Override
    public void regEvent() {
        if (mLLWeChat != null) {
            mLLWeChat.setOnClickListener(this);
        }
        if (mLLWeChatMoments != null) {
            mLLWeChatMoments.setOnClickListener(this);
        }
        if (mLLQQ != null) {
            mLLQQ.setOnClickListener(this);
        }
        if (mLLQQZone != null) {
            mLLQQZone.setOnClickListener(this);
        }
        if (mIvClose != null) {
            mIvClose.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int id =  v.getId();
        if(R.id.shareapp_wechat_ll == id){
            share(2);
        }else if(R.id.shareapp_pengyouquan_ll == id){
            share(3);
        }else if(R.id.shareapp_qq_ll == id){
            share(4);
        }else if(R.id.shareapp_qzone_ll == id){
            share(1);
        }else if(R.id.iv_share_dismiss == id){
            dismiss();
        }
    }

    /**
     * 分享
     *
     * @param flag 标记
     */
    private void share(int flag) {
        if (mShareParams == null) {
            return;
        }

        UMWeb web;
        ShareAction shareAction = new ShareAction((Activity) context);

        if (flag == 1) {
            shareAction.setPlatform(SHARE_MEDIA.QZONE);  //qq空间
        } else if (flag == 2) {
            shareAction.setPlatform(SHARE_MEDIA.WEIXIN);  //微信
        } else if (flag == 3) {
            shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);  //朋友圈
        } else {
            shareAction.setPlatform(SHARE_MEDIA.QQ);  //QQ
        }
        web = new UMWeb(mShareParams.mUrl);
        web.setTitle(mShareParams.mTitle);
        if(TextUtils.isEmpty(mShareParams.mHeadUrl)){ //判断服务端是否传 分享的icon
            File file = new File(ICON_DIR + "/logoshare.jpg");
            web.setThumb(new UMImage(context, file));//没有就用本地logo
        }else{
            web.setThumb(new UMImage(context, mShareParams.mHeadUrl));
        }

        web.setDescription(mShareParams.mContent);
        shareAction.setCallback(new UMShareListener() {
            /**
             * @descrption 分享开始的回调
             * @param platform 平台类型
             */
            @Override
            public void onStart(SHARE_MEDIA platform) {

            }

            /**
             * @descrption 分享成功的回调
             * @param platform 平台类型
             */
            @Override
            public void onResult(SHARE_MEDIA platform) {
                showToast(getContext(), "分享成功", 0);
                if (mCallback != null) {
                    mCallback.shareSuccess();
                }
            }

            /**
             * @descrption 分享失败的回调
             * @param platform 平台类型
             * @param t 错误原因
             */
            @Override
            public void onError(SHARE_MEDIA platform, Throwable t) {
                showToast(getContext(), "分享失败", 0);
                if (mCallback != null) {
                    mCallback.shareFailed();
                }
            }

            /**
             * @descrption 分享取消的回调
             * @param platform 平台类型
             */
            @Override
            public void onCancel(SHARE_MEDIA platform) {
                showToast(getContext(), "分享取消", 0);
                if (mCallback != null) {
                    mCallback.shareFailed();
                }
            }
        });

        dismiss();
        shareAction.withMedia(web);
        shareAction.share();
    }

    public void setCallback(ShareCallback callback) {
        this.mCallback = callback;
    }

    public interface ShareCallback {

        void shareSuccess();

        void shareFailed();
    }

    private String getDiskFileDir() {
        String cachePath = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {

            File externalFilesDir = context.getExternalFilesDir("");
            if (externalFilesDir != null) {
                cachePath = externalFilesDir.getPath();
            } else {
                cachePath = context.getFilesDir().getPath();
            }
        } else {
            cachePath = context.getFilesDir().getPath();
        }
        return cachePath;
    }
}
